/**
 * Created by ZFL on 2017/12/21.
 */
function appInit(){
    PageInit()
}
var vm=new Vue({
    el:'#home',
    data:{
        tabList:[],      //tab列表
        tabImglist:[],   //tab页的轮播图
        noticeList:[],   //消息列表
        tRecommendList:[],    //今日推荐列表
        classifyList:[],    //分类列表
        tabGoodslist:[],    //关联商品列表
    },
    methods:{
        setAttr:function(){
          var _this=this;
            _this.tabList.forEach(function(val,index){
                _this.$set(val,'clicked',false);
            })
        },
        clickTab:function(id,type,val,ind){
            //id代表tab页id  type代表跳到tab的入口类型 -1代表直接点击tab标签   4代表从别处跳转到tab标签
            if(type==4) {
                vm.tabList.forEach(function (value, index) {
                    value.clicked = false;
                })   //把所有的clicked全置为false
                vm.tabList.forEach(function (value, index) {

                    if (value.indexTabId == id) {
                        var index = index + 1;
                        value.clicked = true;
                        $('.tab-' + index).addClass('isShow').siblings('.tabContent').removeClass('isShow');
                        indexTabId = id;
                    }
                })
            }else if(type==-1){
                var index=ind+1;
                vm.tabList.forEach(function(value,index){
                    value.clicked=false;
                })
                val.clicked=true;
                $('.tab-'+index).addClass('isShow').siblings('.tabContent').removeClass('isShow');
                indexTabId=val.indexTabId;

            }

            if(type==4||type==-1){

                vm.tabGoodslist=[];   //每点击一次  清除关联商品数组
                pageNum2=1;          //每点击一次将当前页数置为1
                swiper(indexTabId);   //点击不同tab显示不同轮播图
                getTabgoods();   //点击显示不同的关联商品
                if(index!==1){
                    //滚动加载
                    infiniteSroll2();
                }

            }

        }
    }
})

function PageInit(){
    //if($.getStorage('xmOpenid')&& $.isWX()){
    //    $.updataInfo($.getStorage('xmOpenid'))
    //}
    if($.getStorage('hx_address')){
        document.getElementById("address").innerHTML = $.getStorage('hx_address');
    }
        getTablist();     //tab列表
        sendNotice();      //消息通知
        recommend();        //今日推荐
        category();         //精选分类
        infiniteSroll1();   //滚动加载(今日推荐)
        //如果链接带有talentId   则直接存储
        if ($.getPara('talentId')) {
            window.localStorage.setItem("talentId", $.getPara('talentId'));
        }

        $(window).scroll(function () {
            //这个方法是当前滚动条滚动的距离
            var pageTop = $(window).scrollTop();
            //console.log(pageTop);
            if (pageTop >= 500) {
                $('.return-top').fadeIn();
                $('.return-search').fadeIn();
            } else {
                $('.return-top').fadeOut();
                $('.return-search').fadeOut();
            }
        });

    }

var indexTabId=0;    //tab页id


//得到tab列表
function getTablist(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            return obj;
        },
        visibleTime: 100,
        visible: 'hide',
        dataType: "json",
        api:'yg_baijiaappgoods/indexTab/tabList',
        success:function(result){
            if(result.code==10000){
                vm.tabList=result.data;
                var tab_allW=result.data.length*2.5+'rem';    //tab页的宽度
                $('.tab_all').css({
                    'width':tab_allW
                })
                vm.setAttr();

                vm.tabList.forEach(function(val,ind){
                    //clicked表示tab页是否被点击
                    if(ind==0){
                        val.clicked=true;
                        swiper(val.indexTabId);           //轮播图
                    }else{
                        val.clicked=false;
                    }
                })
            }
        }


    })
}
//轮播图
function swiper(indexTabId){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.indexTabId=indexTabId;
            console.log(obj)
            return obj;
        },
        visibleTime: 100,
        dataType: "json",
        api:'yg_baijiaapp/circulationPicture/selectCirculationPicture',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                console.log(data)
                var html='';
                $.each(data,function (index,item) {
                    if(item.type==1){
                        //商品
                        html += '<div class="swiper-slide"><a href="/b2c/goods/goods.html?goodsId='+item.activity+'"><img src="'+$.imgApi()+''+item.image+'"></a></div>';
                    }else if(item.type==2){
                        //期
                        html += '<div class="swiper-slide"><a href="/b2c/recommend/index.html?serialId='+item.activity+'"><img src="'+$.imgApi()+''+item.image+'"></a></div>';
                    }else if(item.type==3){
                        //外链活动
                        html += '<div class="swiper-slide"><a href="'+item.activity+'"><img src="'+$.imgApi()+''+item.image+'"></a></div>';
                    }else if(item.type==4){
                        //跳转到tab页
                        //html += '<div class="swiper-slide"><a href="javascript:void(0)"><img src="'+$.imgApi()+''+item.image+'"></a></div>';
                        html+='<div class="swiper-slide"><a href="javascript:void(0)" @click=clickTab('+item.activity+','+item.type+')><img src="'+$.imgApi()+''+item.image+'"></a></div>'
                    }else{
                        html += '<div class="swiper-slide"><a href="javascript:void(0)"><img src="'+$.imgApi()+''+item.image+'"></a></div>';
                    }
                    vm.tabImglist.push(item)

                });
                $('.swiper-wrapper-1').html(html);
                var swiper = new Swiper('.swiper-container-1', {
                    pagination: '.swiper-pagination-1',
                    paginationClickable: true,
                    loop: true,
                    autoplay : 3000
                });

            }else{
                $.toast('加载失败');
            }
        }
    })
}
//消息通知
function sendNotice(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            return obj;
        },
        visibleTime: 100,
        visible: 'hide',
        dataType: "json",
        api:'yg_baijiaapp/pushMessage/getNotice',
        success:function(result){
            if(result.code==10000){
                var data=result.data;
                var html='';
                if(data.length>0){
                    data.forEach(function(value,index){
                        html='<li>'+value.content+'</li>'
                        $('.notice ul').append(html)
                    })
                }else{
                    $('.notice').css({
                        'display':'none'
                    })
                }

            }
        }
    })
}
var timer=null;
var $uList = $(".notice ul")
timer = setInterval(function () {
    scrollList($uList);
},3000)
function scrollList(obj) {;
    //获得当前<li>的高度
    var scrollHeight = $(".notice ul li:first").height();
    //滚动出一个<li>的高度
    $uList.stop().animate(
        {'margin-top':-scrollHeight},1500,function () {
        //动画结束后，将当前<ul>marginTop置为初始值0状态，再将第一个<li>拼接到末尾。
        $uList.css({'margin-top':0}).find("li:first").appendTo($uList);
    });
}


var pageNum1=1;
var pageSize1=10;

//今日推荐
function  recommend(){
    var obj={};
    if (com_obj.userId &&com_obj.userId>0) {
        obj.userId = com_obj.userId;
    } else {
        obj.userId = 0;
    }
    obj.pageNum=pageNum1;
    obj.pageSize=pageSize1;
    $.ajaxHandler({

        model:function(){
            console.log(obj)
            return obj;
        },
        visibleTime: 100,
        visible: 'hide',
        dataType: "json",
        api: 'yg_baijiaapp/serial/todayRecommend',
        success:function(result){
            if(result.code==10000){
                var data = result.data;
                if(data.length>0){
                    // 加载数据
                    loading = false;
                data.forEach(function(value,index){
                        value.goodsList.forEach(function(val,ind){
                            val.lowestPrice=divideNumber(val.lowestPrice,100);
                            val.suggestedPrice=divideNumber(val.suggestedPrice,100);

                        })
                        if(value.jumpStyle==1){
                            //不跳转
                            value.href='javascript:;'
                        }else if(value.jumpStyle==2){
                            //单品   跳转商品详情
                            value.href="/b2c/goods/goods.html?goodsId="+value.jumpTo;
                        }else if(value.jumpStyle==3){
                            //跳转到指定url
                            value.href=value.jumpTo;
                        }else if(value.jumpStyle==4){
                            //跳转到另一个期资讯详情
                            value.href='b2c/recommend/index.html?serialId='+value.jumpTo;
                        }
                        value.width=value.goodsList.length*3+3+'rem';
                        vm.tRecommendList.push(value);
                    })




                }else{
                    $(document.body).destroyInfinite();
                    //没有今日推荐
                    //$(".recommend").hide(); //今日推荐隐藏
                }

            }else if(result.code == 20160){
                $.toast('没有此用户', 'text');
            }else {
                $.toptip(result.message, 'warning');
            }
        }

    })
}

//滚动加载(今日推荐)
function infiniteSroll1() {
    loading1 = false;
    $("body").infinite().on("infinite", function () {
        if (loading1) return;
        loading1 = true;
        pageNum1++;
        $('.weui-infinite-scroll-box').show();
        setTimeout(function () {
            loading1 = false;
            $('.weui-infinite-scroll-box').hide();
            recommend();
        }, 1500);   //模拟延迟
    });
}


var pageNum2=1;
var pageSize2=10;
var loading2;   //状态标记
//关联商品列表
function getTabgoods(){
    $.ajaxHandler({
        model:function(){
            var obj={}
            obj.indexTabId=indexTabId;
            obj.pageNum=pageNum2;
            obj.pageSize=pageSize2;
            obj.userId=com_obj.userId;
            console.log(obj)
            return obj;
        },
        visibleTime: 100,
        visible:'hide',
        dataType: "json",
        api:'yg_baijiaappgoods/indexTab/indexTabGoods',
        success:function(result){
            if(result.code==10000){
                var data=result.data;
                if(data.length>0){
                    // 加载数据
                    loading2 = false;
                    var data=result.data;
                    data.forEach(function(value,index){
                        value.lowestPrice=divideNumber(value.lowestPrice,100);
                        value.suggestedPrice=divideNumber(value.suggestedPrice,100);
                        vm.tabGoodslist.push(value);
                    })
                }else{
                    $(document.body).destroyInfinite();
                }

            }
        }

    })
}
//版本推荐
function category(){
    $.ajaxHandler({
            model: function () {
            },
            visibleTime: 100,
            visible:'hide',
            dataType: "json",
            api:'yg_baijiaappgoods/indexTab/indexCategory',
            success: function (result) {
                //console.log(result);
                var data = result.data;
                if (result.code == 10000) {

                    data.forEach(function(val,ind){
                        if(val.type==1){
                            //跳转商品
                            val.href='b2c/goods/goods.html?goodsId='+val.activity;
                        }else if(val.type==2){
                            //跳转期
                            val.href='b2c/recommend/index.html?serialId='+val.activity;
                        }else if(val.type==3){
                            //其他活动url
                            val.href=val.activity;
                        }else if(val.type==4){
                            //tab页


                        }else if(val.type==5){
                            //二级分类
                            val.href='b2c/classes/detail.html?categoryId='+val.activity+'&categoryType=1';
                        }else if(val.type==6){
                            //三级分类
                            val.href='b2c/classes/detail.html?categoryId='+val.activity+'&categoryType=0';
                        }

                    })
                    vm.classifyList=data;   //分类列表
                    console.log( vm.classifyList)

                }else {
                    $.toast('精选分类异常');
                }

            }
        }
    );
}



//滚动加载(关联商品)
function infiniteSroll2() {
     loading2 = false;
    $("body").infinite().on("infinite", function () {
        if (loading2) return;
        loading2 = true;
        pageNum2++;
        $('.weui-infinite-scroll-box').show();
        setTimeout(function () {
            loading2 = false;
            $('.weui-infinite-scroll-box').hide();
            getTabgoods(indexTabId)
        }, 1500);   //模拟延迟
    });
}

//返回顶部
function returnTop(){
    //绑定回到顶部按钮的点击事件
    //动画效果，平滑滚动回到顶部
    //window.scrollTo(0,0)
    $('body,html').animate({scrollTop:0},400,'swing');
}
//跳转搜索
function returnSearch() {
    window.location.href ='/b2c/search/index.html';
}

//点击地图图标   重新定位
$('.userLocation').click(function(){
    getLocaltion();
})
function getLocaltion(){
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(108.95,34.27);
    map.centerAndZoom(point,12);

    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function(r){console.log(r.point)
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);//标出所在地
            map.panTo(r.point);//地图中心移动
            window.localStorage.setItem('userlatitude',r.point.lat);
             window.localStorage.setItem('userlongitude',r.point.lng);
            var point = new BMap.Point(r.point.lng,r.point.lat);//用所定位的经纬度查找所在地省市街道等信息
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function(rs){
                var addComp = rs.addressComponents;
                console.log(JSON.stringify(rs));//地址信息
                document.getElementById("address").innerHTML = rs.address;
                window.localStorage.setItem('hx_address',rs.address);
                YDUI.dialog.toast('定位成功',1000)

            });
        }else {
            alert('failed'+this.getStatus());
        }
    },{enableHighAccuracy: true})
}

var obj={};
var arr=[1,2,3];
obj=arr;
console.log(obj)